package com.bawei.image.loader

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.net.Uri
import android.widget.ImageView
import com.bawei.image.IImgLoader
import com.bawei.image.ImgOptions
import com.squareup.picasso.MemoryPolicy
import com.squareup.picasso.NetworkPolicy
import com.squareup.picasso.Picasso
import com.squareup.picasso.RequestCreator
import com.squareup.picasso.Transformation
import java.io.File

/**
 * Author: Jason Zhang
 * Date: 2025/1/10
 * Desc: Picasso图片加载器
 */
class PicassoImgLoader: IImgLoader {
    override fun loadFileIntoImageView(
        ctx: Context,
        file: File,
        target: ImageView,
        option: ImgOptions
    ) {
        var creater =  Picasso.get().load(file)
        loadImage(creater, target, file.name, option)
    }

    override fun loadUrlIntoImageView(
        ctx: Context,
        url: String,
        target: ImageView,
        option: ImgOptions
    ) {
        var creator =  Picasso.get().load(url)
        loadImage(creator, target, url, option)
    }

    override fun loadResIntoImageView(
        ctx: Context,
        resId: Int,
        target: ImageView,
        option: ImgOptions
    ) {
        var creator =  Picasso.get().load(resId)
        loadImage(creator, target, "", option)
    }

    override fun loadByteArrayIntoImageView(
        ctx: Context,
        byteArray: ByteArray,
        target: ImageView,
        option: ImgOptions
    ) {

    }

    override fun loadUriIntoImageView(
        ctx: Context,
        uri: Uri,
        target: ImageView,
        option: ImgOptions
    ) {
        var creator =  Picasso.get().load(uri)
        loadImage(creator, target, "", option)
    }


    private fun loadImage(creator: RequestCreator, target: ImageView, key: String, option: ImgOptions){
        if (option.placeImage > 0){
            creator.placeholder(option.placeImage)
        }

        if (option.errorImage > 0){
            creator.error(option.errorImage)
        }

        if (option.isCircle){
            creator.centerCrop().transform(object : Transformation{
                override fun transform(source: Bitmap): Bitmap {
                    val canvas = Canvas(source)
                    // 对图片进行自定义处理,可以参考Glide中TransformationUtils的实现
                    val result = Bitmap.createScaledBitmap(source, 200, 200, false) //例子
                    source.recycle()
                    return result
                }

                override fun key(): String {
                    return key
                }

            })
        }

        //是否开启内存缓存
        if(option.skipMemory){
            creator.memoryPolicy(MemoryPolicy.NO_CACHE)
        }

        //是否开启磁盘缓存
        if (option.skipDiskCache){
            creator.memoryPolicy(MemoryPolicy.NO_STORE)
        }

        //是否开启网络缓存
        creator.networkPolicy(NetworkPolicy.OFFLINE) //离线缓存

        //设置图片大小
        if (option.imgSize.size == 2){
            creator.resize(option.imgSize[0], option.imgSize[1])
        }
        creator.into(target)

    }



}